package action.FrontAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.*;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.*;
import vo.*;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin on 2017/5/18.
 */
public class PurchaseAction extends ActionSupport {

    @Autowired
    private CroPurchaseService croPurchaseService;
    @Autowired
    private CroBuycarsService croBuycarsService;
    @Autowired
    private CroChangegoodService croChangegoodService;
    @Autowired
    private CroReturngoodsService croReturngoodsService;
    @Autowired
    private CroPurgoodsService croPurgoodsService;

    private List<CroPurchaseVo> purchaseVoList;
    private String title;
    private String cusId;
    private String carId;
    private String perId;
    private String goodId;
    private String gtype;
    private String bgoodId;
    private String oldNum;
    private String newNum;
    private String reson;
    private String jsonResult;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;
    private Map condition;

    /**
     * 根据订单Id搜索
     * @return
     */
    public String purchaseId(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("id",perId);
        maps.put("del_flag","0");
        maps.put("order_flag","1");
        CroPurchaseVo Vo = croPurchaseService.listByProperty(maps).get(0);
        List<CroBuycars> buycars = croBuycarsService.listByPurchase(croPurchaseService.findById(perId));
        Vo.setBuycarsList(buycars);
        purchaseVoList.add(Vo);
        title = "list";
        return SUCCESS;
    }

    /**
     * 获取订单中商品的详细信息
     * @return
     */
    public String goodsdetail(){
        CroBuycars croBuycars = croBuycarsService.findById(carId);
        BuyCargoodsVo buyCargoodsVo = new BuyCargoodsVo();
        ParseBuycarVo parseBuycarVo = JSON.parseObject(croBuycars.getGoodsdetail(),ParseBuycarVo.class);
        buyCargoodsVo.setId(croBuycars.getId());
        buyCargoodsVo.setName(parseBuycarVo.getTitle());
        StringBuffer sb = new StringBuffer();
        for(GoodsPropertiesNameVo goodsdetail:parseBuycarVo.getPropertiesName()){
            sb.append(goodsdetail.getPropName()+":"+goodsdetail.getPropValueName()+", ");
        }
        buyCargoodsVo.setGoodsId(croBuycars.getCroPurgoods().getId());
        buyCargoodsVo.setDescrib(sb.toString());
        buyCargoodsVo.setPhoto(parseBuycarVo.getPicUrl());
        buyCargoodsVo.setPrice(parseBuycarVo.getPrice());
        buyCargoodsVo.setActprice(croBuycars.getActualprice());
        buyCargoodsVo.setNumbers(croBuycars.getNumbers());
        buyCargoodsVo.setSumprice(croBuycars.getSumprice());
        jsonResult = JSON.toJSONString(buyCargoodsVo);
        return SUCCESS;
    }

    /**
     * 退换货申请
     * @return
     */
    public String returnPolicy(){
        CroPurchase purchase = croPurchaseService.findById(perId);
        CroPurgoods purgoods = croPurgoodsService.getPurgoodsById(goodId);
        if (gtype.equals("1")){
            //退货
            CroReturngoods goods = new CroReturngoods();
            goods.setCroPurchase(purchase);
            goods.setCroPurgoods(purgoods);
            goods.setGoodsnumber(oldNum);
            goods.setReturnreason(reson);
            if (croReturngoodsService.addReturnGoods(goods)){
                jsonResult = "{\"status\":1,\"msg\":\"退货申请提交成功!\"}";
            }else{
                jsonResult = "{\"status\":0,\"msg\":\"申请提交失败!\"}";
            }
        }else if (gtype.equals("2")){
            //换货
            CroPurgoods tmppurgoods = croPurgoodsService.getPurgoodsById(bgoodId);
            CroChangegoods goods = new CroChangegoods();
            goods.setCroPurchase(purchase);
            goods.setCroPurgoods(purgoods);
            goods.setChangeCroPurgoods(tmppurgoods);
            goods.setGoodsnumber(oldNum);
            goods.setChangegoodsnumber(newNum);
            if (croChangegoodService.addReturnGoods(goods)){
                jsonResult = "{\"status\":1,\"msg\":\"换货申请提交成功!\"}";
            }else{
                jsonResult = "{\"status\":0,\"msg\":\"申请提交失败!\"}";
            }
        }
        return SUCCESS;
    }

    /**
     * 全部订单
     * @return
     */
    public String alls(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("del_flag","0");
        Pagination<CroPurchaseVo> page = new Pagination<CroPurchaseVo>();

        if (pageIndex == 0)//前台没有传值的情况
        {
            page = croPurchaseService.listPageByProperty(maps,1,pageSize);

        } else {
            page = croPurchaseService.listPageByProperty(maps,pageIndex,pageSize);
        }
        for (CroPurchaseVo vo: page.getItems()){
            List<CroBuycars> buycars = croBuycarsService.listByPurchaseId(vo.getId());
            vo.setBuycarsList(buycars);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        setPurchaseVoList(page.getItems());
        title = "alls";
        return "list";
    }

    /**
     * 待发货订单
     * @return
     */
    public String payed(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("orderStatus","2");
        maps.put("del_flag","0");
        Pagination<CroPurchaseVo> page = new Pagination<CroPurchaseVo>();

        if (pageIndex == 0)//前台没有传值的情况
        {
            page = croPurchaseService.listPageByProperty(maps,1,pageSize);

        } else {
            page = croPurchaseService.listPageByProperty(maps,pageIndex,pageSize);
        }
        for (CroPurchaseVo vo: page.getItems()){
            List<CroBuycars> buycars = croBuycarsService.listByPurchaseId(vo.getId());
            vo.setBuycarsList(buycars);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        setPurchaseVoList(page.getItems());
        title = "payed";
        return "list";
    }

    /**
     * 待收货订单
     * @return
     */
    public String sent(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("orderStatus","3");
        maps.put("del_flag","0");
        Pagination<CroPurchaseVo> page = new Pagination<CroPurchaseVo>();

        if (pageIndex == 0)//前台没有传值的情况
        {
            page = croPurchaseService.listPageByProperty(maps,1,pageSize);

        } else {
            page = croPurchaseService.listPageByProperty(maps,pageIndex,pageSize);
        }
        for (CroPurchaseVo vo: page.getItems()){
            List<CroBuycars> buycars = croBuycarsService.listByPurchaseId(vo.getId());
            vo.setBuycarsList(buycars);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        setPurchaseVoList(page.getItems());
        title = "sent";
        return "list";
    }

    /**
     * 待付款订单
     * @return
     */
    public String waits(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("orderStatus","0");
        maps.put("del_flag","0");
        Pagination<CroPurchaseVo> page = new Pagination<CroPurchaseVo>();

        if (pageIndex == 0)
        {
            page = croPurchaseService.listPageByProperty(maps,1,pageSize);

        } else {
            page = croPurchaseService.listPageByProperty(maps,pageIndex,pageSize);
        }
        for (CroPurchaseVo vo: page.getItems()){
            List<CroBuycars> buycars = croBuycarsService.listByPurchaseId(vo.getId());
            vo.setBuycarsList(buycars);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        setPurchaseVoList(page.getItems());
        title = "waits";
        return "list";
    }

    /**
     * 已完成订单
     * @return
     */
    public String done(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("orderStatus","4");
        maps.put("del_flag","0");
        Pagination<CroPurchaseVo> page = new Pagination<CroPurchaseVo>();

        if (pageIndex == 0)//前台没有传值的情况
        {
            page = croPurchaseService.listPageByProperty(maps,1,pageSize);

        } else {
            page = croPurchaseService.listPageByProperty(maps,pageIndex,pageSize);
        }
        for (CroPurchaseVo vo: page.getItems()){
            List<CroBuycars> buycars = croBuycarsService.listByPurchaseId(vo.getId());
            vo.setBuycarsList(buycars);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        setPurchaseVoList(page.getItems());
        title = "done";
        return "list";
    }

    /**
     * 顾客删除个人订单
     * @return
     */
    public String delper(){
        if (croPurchaseService.userDelPurchase(perId)){
            jsonResult = "{\"status\":1,\"msg\":\"删除成功!\"}";
        }else{
            jsonResult = "{\"status\":0,\"msg\":\"删除失败!\"}";
        }
        return "delper";
    }

    /**
     * 顾客完成订单
     * @return
     */
    public String perdone(){
        if (croPurchaseService.donePurchase(perId)){
            jsonResult = "{\"status\":1,\"msg\":\"交易成功!\"}";
        }else{
            jsonResult = "{\"status\":0,\"msg\":\"交易失败!\"}";
        }
        return "perdone";
    }

    public List<CroPurchaseVo> getPurchaseVoList() {
        return purchaseVoList;
    }

    public void setPurchaseVoList(List<CroPurchaseVo> purchaseVoList) {
        this.purchaseVoList = purchaseVoList;
    }

    public String getCusId() {
        return cusId;
    }

    public void setCusId(String cusId) {
        this.cusId = cusId;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Map getCondition() {
        return condition;
    }

    public void setCondition(Map condition) {
        this.condition = condition;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setJsonResult(String jsonResult) {
        this.jsonResult = jsonResult;
    }

    public String getPerId() {
        return perId;
    }

    public void setPerId(String perId) {
        this.perId = perId;
    }

    public String getCarId() {
        return carId;
    }

    public void setCarId(String carId) {
        this.carId = carId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGtype() {
        return gtype;
    }

    public void setGtype(String gtype) {
        this.gtype = gtype;
    }

    public String getBgoodId() {
        return bgoodId;
    }

    public void setBgoodId(String bgoodId) {
        this.bgoodId = bgoodId;
    }

    public String getOldNum() {
        return oldNum;
    }

    public void setOldNum(String oldNum) {
        this.oldNum = oldNum;
    }

    public String getNewNum() {
        return newNum;
    }

    public void setNewNum(String newNum) {
        this.newNum = newNum;
    }

    public String getGoodId() {
        return goodId;
    }

    public void setGoodId(String goodId) {
        this.goodId = goodId;
    }

    public String getReson() {
        return reson;
    }

    public void setReson(String reson) {
        this.reson = reson;
    }
}
